﻿package view.tips
{
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.text.*;
    import flash.utils.*;

    public class FlashToolTip extends Sprite
    {
        public var _multiLine:Boolean = false;
        public var labelMargin:Number = 10;
        public var arrowMargin:Number = 10;
        public var showDelay:Number = 0.1;
        public var hideDelay:Number = 0.1;
        private var _label:String;
        private var _fixedWidth:Number = 50;
        private var _horDirection:String;
        private var _verticalDirection:String;
        private var _followMouse:Boolean;
        private var _timer:Timer;
        private var _xposition:Number;
        private var _yposition:Number;
        private var _stageWidth:Number;
        private var _stageHeight:Number;
        private var _labelColor:Number;
        private var label_txt:TextField;
        private var _bg:BitmapScale9Grid;
        private var bgClass:Class;
        private static var instance:FlashToolTip;
        private static var allowInstance:Boolean;
        private static const DIRECTION_LEFT:String = "directionleft";
        private static const DIRECTION_RIGHT:String = "directionright";
        private static const DIRECTION_TOP:String = "directiontop";
        private static const DIRECTION_BOTTOM:String = "directionbottom";

        public function FlashToolTip()
        {
            this.bgClass = FlashToolTip_bgClass;
            if (!allowInstance)
            {
                throw new Error("use ToolTip.getInstance() instead of new keyword !");
            }
            return;
        }// end function

        public function set multiLine(param1:Boolean) : void
        {
            this._multiLine = param1;
            if (this._multiLine)
            {
                this.label_txt.multiline = true;
                this.label_txt.wordWrap = true;
                this.label_txt.width = this._fixedWidth;
                this.label_txt.autoSize = TextFieldAutoSize.RIGHT;
            }
            else
            {
                this.label_txt.width = 40;
                this.label_txt.multiline = false;
                this.label_txt.wordWrap = false;
                this.label_txt.autoSize = TextFieldAutoSize.LEFT;
            }
            return;
        }// end function

        public function set fixedWidth(param1:Number) : void
        {
            this._fixedWidth = Math.max(param1, 50);
            return;
        }// end function

        public function set labelColor(param1:Number) : void
        {
            this._labelColor = param1;
            this.label_txt.textColor = this._labelColor;
            return;
        }// end function

        public function get labelColor() : Number
        {
            return this._labelColor;
        }// end function

        private function init() : void
        {
            this.mouseChildren = false;
            this.mouseEnabled = false;
            this._bg = new BitmapScale9Grid(new this.bgClass(), 5, 16, 5, 29);
            this.addChild(this._bg);
            this.label_txt = new TextField();
            this.addChild(this.label_txt);
            this.label_txt.filters = [new GlowFilter(0, 1, 2, 2, 5)];
            this.label_txt.x = this.labelMargin;
            this.label_txt.y = 4;
            this.label_txt.autoSize = TextFieldAutoSize.LEFT;
            this.label_txt.defaultTextFormat = new TextFormat("宋体", null, null, null, null, null, null, null, null, null, null, null, 5);
            this._horDirection = DIRECTION_RIGHT;
            this._verticalDirection = DIRECTION_TOP;
            addEventListener(Event.ADDED_TO_STAGE, this.onAdded, false, 0, true);
            this.reSize();
            return;
        }// end function

        private function onAdded(event:Event) : void
        {
            removeEventListener(Event.ADDED_TO_STAGE, this.onAdded);
            this.onStageResize();
            stage.addEventListener(Event.RESIZE, this.onStageResize, false, 0, true);
            return;
        }// end function

        private function onStageResize(event:Event = null) : void
        {
            this._stageWidth = stage.stageWidth;
            this._stageHeight = stage.stageHeight;
            return;
        }// end function

        private function reSize() : void
        {
            return;
        }// end function

        public function show(param1:String = "", param2:Number = 0, param3:Number = 0) : void
        {
            this.multiLine = false;
            this._label = param1;
            this.label_txt.textColor = this.labelColor;
            this.label_txt.htmlText = this._label;
            if (this.label_txt.width > this._fixedWidth)
            {
                this.multiLine = true;
            }
            this._xposition = 10;
            this._yposition = 10;
            if (this._timer)
            {
                this._timer.stop();
                this._timer.removeEventListener(TimerEvent.TIMER, this.onTimer);
                this._timer = null;
            }
            this.updatePosition();
            TweenLite.killTweensOf(this);
            TweenLite.to(this, this.showDelay, {alpha:1, ease:Linear.easeNone});
            this._bg.width = this.label_txt.width + this.labelMargin * 2;
            this._bg.height = this.label_txt.height + 10;
            if (this._followMouse)
            {
                this._timer = new Timer(20);
                this._timer.addEventListener(TimerEvent.TIMER, this.onTimer, false, 0, true);
                this._timer.start();
            }
            return;
        }// end function

        private function updatePosition() : void
        {
            var _loc_1:* = this._followMouse ? (stage.mouseX + this._xposition) : (this._xposition);
            var _loc_2:* = this._followMouse ? (stage.mouseY + this._yposition) : (this._yposition);
            this._horDirection = _loc_1 + this._bg.width - this.arrowMargin > this._stageWidth ? (DIRECTION_LEFT) : (DIRECTION_RIGHT);
            this._verticalDirection = _loc_2 + this._bg.height > this._stageHeight ? (DIRECTION_TOP) : (DIRECTION_BOTTOM);
            this.x = Math.round(this._horDirection == DIRECTION_LEFT ? (_loc_1 - this._bg.width + this.arrowMargin) : (_loc_1 - this._bg.x));
            this.y = Math.max(0, Math.round(this._verticalDirection == DIRECTION_TOP ? (_loc_2 - this._bg.height) : (_loc_2)));
            return;
        }// end function

        public function hide() : void
        {
            if (this._timer)
            {
                this._timer.stop();
                this._timer.removeEventListener(TimerEvent.TIMER, this.onTimer);
                this._timer = null;
            }
            TweenLite.killTweensOf(this);
            TweenLite.to(this, this.hideDelay, {alpha:0, ease:Linear.easeNone});
            return;
        }// end function

        private function onTimer(event:TimerEvent) : void
        {
            event.updateAfterEvent();
            this.updatePosition();
            return;
        }// end function

        public function set followMouse(param1:Boolean) : void
        {
            this._followMouse = param1;
            return;
        }// end function

        public function get followMouse() : Boolean
        {
            return this._followMouse;
        }// end function

        public function destroy() : void
        {
            stage.removeEventListener(Event.RESIZE, this.onStageResize);
            if (this._timer)
            {
                this._timer.stop();
                this._timer.removeEventListener(TimerEvent.TIMER, this.onTimer);
                this._timer = null;
            }
            return;
        }// end function

        public static function getInstance() : FlashToolTip
        {
            if (instance == null)
            {
                allowInstance = true;
                instance = new FlashToolTip;
                allowInstance = false;
                ;
            }
            instance.init();
            return instance;
        }// end function

    }
}
